<template>
  <div class="out">
    <div class="category">
      <div class="cat-row">
        <p>语种:</p>
        <span class="cat-item" style="border: none;" @click="changeCurrentArea(-1)">
          <span class="_cat-item" :class="{isactive:params.area === -1}">全部</span>
        </span>
        <span v-for="(item,i) in songArea" class="cat-item" @click="changeCurrentArea(areaIndex[i])">
          <span class="icon">|</span><span class="_cat-item" :class="{isactive:params.area === areaIndex[i]}">{{item}}</span>
        </span>
      </div>
      <div class="cat-row">
        <p>分类:</p>
        <span class="cat-item" style="border: none;" @click="changeCurrentType(-1)">
          <span class="_cat-item"  :class="{isactive:params.type === -1}">全部</span>
        </span>
        <span v-for="(item,i) in songType" class="cat-item" @click="changeCurrentType(i)">
          <span class="icon">|</span><span class="_cat-item" :class="{isactive:params.type === i}">{{item}}</span>
        </span>
      </div>
      <div class="cat-row">
        <p style="width:55px">筛选:</p>
        <div class="shaixuan">
          <span class="cat-item" style="border: none;" @click="changeCurrentCapt(-1)">
            <span class="_cat-item" :class="{isactive:params.initial === -1}">热门</span>
            <span class="icon">|</span>
          </span>
        
          <span v-for="(item,i) in songCapt" class="cat-item" @click="changeCurrentCapt(i)">
            <span class="_cat-item" :class="{isactive:params.initial === item}">{{item}}</span>
            <span class="icon" v-show="i !== songCapt.length - 1">|</span>
          </span>

        </div>
      </div>
    </div>

    <div class="singerList">
      <div class="singerList-item" v-for="item in artists">
        <img :src="item.img1v1Url" width="170" height="170">
        <div class="name">{{item.name}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import {request} from 'network/request'
export default {
  name:'Singer',
  data(){
    return {
      songType:['男歌手','女歌手','乐队组合'],
      songArea:['华语','欧美','日本','韩国','其他'],
      songCapt:['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#'],
      params:{
        limit:30,
        type:-1,
        area:-1,
        initial:-1,
        offset:0
      },
      artists:[],
      hasmore:false,
      areaIndex:[7,96,8,16,0]
    }
  },
  methods:{
    changeCurrentArea(index){
      if(this.params.area !== index ){
        this.params.area = index
        this.offset = 0
        this.getArtist()
      }
    },
    changeCurrentType(index){
      if(this.params.type !== index){
        this.params.type = index
        this.offset = 0
        this.getArtist()
      }
    },
    changeCurrentCapt(index){
      if(index ==  -1 ){
        this.params.initial = -1
        this.getArtist()
      }else if(this.params.initial !== this.songCapt[index]){
        this.params.initial = this.songCapt[index]
        this.offset = 0
        this.getArtist()
      }
    },
    handleArtist(){
      let arr = Object.assign({},this.params)
      if(this.params.initial == '#'){
        arr.initial = 0
      }
      return arr
    },
    async getArtist(){
      let arr = this.handleArtist()
      const data = await request("GET",'artist/list',arr)
      this.artists = data.artists
      this.hasmore = data.more
    },
    async handleScroll(e) {
      if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight) {
        if(this.hasmore){
          this.params.offset += 30
          let arr = this.handleArtist()
          const data = await request("GET",'artist/list',arr)
          this.artists = this.artists.concat(data.artists)
        }
      }
    },
  },
  created(){
    this.getArtist()
  },
  mounted(){
    window.addEventListener('scroll',this.handleScroll,true) 
  },
  destroyed(){
    window.removeEventListener('scroll',this.handleScroll)
  }
}
</script>

<style scoped>
  .out{
    margin: 0 90px;
    font-size: 12px;
    margin-bottom: 30px;
  }
  .category{
    padding-top: 7px;
  }
  .cat-row{
    display: flex;
    height: 18px;
    line-height: 18px;
    margin-bottom: 10px;
  }
  .cat-row p{
    height: 18px;
    line-height: 18px;
    color: var(--font-color);
  }
  .cat-item{
    width: 80px;
    display: flex;
    justify-content:center;
    align-items: center;
    position: relative;
    cursor: pointer;
  }
  .cat-item:hover ._cat-item{
    color: var(--font-color);
  }
  .cat-item:hover .isactive{
    color:var(--netease-color)
  }
  .cat-item .icon{
    position:absolute;
    font-size: 12px;
    left: -2px;
    bottom: 2px;
    color:rgb(242,242,242)
  }
  ._cat-item{
    text-align: center;
    padding: 0 12px;
    height: 18px;
    line-height: 18px;
    border-radius: 30px;
    color: rgb(103,103,103);
    font-weight: 400;
  }
  .isactive{
    background-color: rgb(253,245,245);
    color: var(--netease-color);
  }
  .shaixuan .cat-item{
    width: 65px;
  }
  .shaixuan{
    display: flex;
    flex-wrap: wrap; 
    margin-left: 0px;
    height: 45px;
    align-content: space-between;
  }
  .shaixuan .icon{
    left: 65px;
  }
  .singerList{
    margin-top: 45px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .singerList img{
    border-radius: 5px;
    cursor: pointer;
  }
  .name{
    font-size: 14px;
    color: var(--font-color);
    margin: 5px 0 15px;
  }
</style>